*************************************
* GENERATING MONADIC TRADE ABBA TERMS
*************************************

use mirrortrade-uni-wgtavgs

* generate counter

generate num=1

* generate countryyear variable

generate cowcodeAmult=(cowcodeAuni*10000)
generate countryyearA=(cowcodeAmult+year)
format countryyearA %7.0f

generate cowcodeBmult=(cowcodeBuni*10000)
generate countryyearB=(cowcodeBmult+year)
format countryyearB %7.0f

**************************
* IMPORTS (flow 1, B to A)
**************************

* counters

sort countryyearA cowcodeBuni

by countryyearA: egen num_obs=sum(num)

by countryyearA: egen num_impBAA=sum(num) if imf_flow1_uni!=.
by countryyearA: egen num_impBAB=sum(num) if imf_flow1mirror_uni!=.
by countryyearA: generate consistent_impBA=1 if num_impBAA!=. & num_impBAB!=.
by countryyearA: generate consistentnopr_impBA=1 if consistent_impBA==1 & pr_imf_flow1_uni==0 & pr_imf_flow1mirror_uni==0

* values

by countryyearA: egen totval_impBAA=sum(imf_flow1_uni)
by countryyearA: egen totval_impBAB=sum(imf_flow1mirror_uni)

by countryyearA: egen totval_impBAA_cons=sum(imf_flow1_uni) if consistent_impBA==1
by countryyearA: egen totval_impBAA_cons2=max(totval_impBAA_cons)

by countryyearA: egen totval_impBAB_cons=sum(imf_flow1mirror_uni) if consistent_impBA==1
by countryyearA: egen totval_impBAB_cons2=max(totval_impBAB_cons)

by countryyearA: egen totval_impBAA_consnopr=sum(imf_flow1_uni) if consistentnopr_impBA==1
by countryyearA: egen totval_impBAA_consnopr2=max(totval_impBAA_consnopr)

by countryyearA: egen totval_impBAB_consnopr=sum(imf_flow1mirror_uni) if consistentnopr_impBA==1
by countryyearA: egen totval_impBAB_consnopr2=max(totval_impBAB_consnopr)

label variable totval_impBAA_consnopr2 "Monadic aggregate mirror imports, home"
label variable totval_impBAB_consnopr2 "Monadic aggregate mirror imports, partners"

*SAVE IMPORTS

keep if cowcodeBuni==2 | cowcodeBuni==20
drop if cowcodeBuni==20 & cowcodeBuni!=2

keep cowcodeAuni countryA_name_uni year totval_impBAA_consnopr2 totval_impBAB_consnopr2

save "C:\Users\lukas\Dropbox\Mirror replication paper\replication materials\messy\monadic\monadicimports.dta"


**************************
* EXPORTS (flow 2, A to B)
**************************

use mirrortrade-uni-wgtavgs

* generate counter

generate num=1

* generate countryyear variable

generate cowcodeAmult=(cowcodeAuni*10000)
generate countryyearA=(cowcodeAmult+year)
format countryyearA %7.0f

generate cowcodeBmult=(cowcodeBuni*10000)
generate countryyearB=(cowcodeBmult+year)
format countryyearB %7.0f

* Note that, compared to imports, A and B switch places:  B=home&sender; A=partner&receiver -> flow1=partner; flow1mirror="home"

* counters

sort countryyearB cowcodeAuni

by countryyearB: egen num_obs_exp=sum(num)

by countryyearB: egen num_expBAA=sum(num) if imf_flow1_uni!=.
by countryyearB: egen num_expBAB=sum(num) if imf_flow1mirror_uni!=.
by countryyearB: generate consistent_expBA=1 if num_expBAA!=. & num_expBAB!=.
by countryyearB: generate consistentnopr_expBA=1 if consistent_expBA==1 & pr_imf_flow1_uni==0 & pr_imf_flow1mirror_uni==0

* values

by countryyearB: egen totval_expBAA=sum(imf_flow1_uni)
by countryyearB: egen totval_expBAB=sum(imf_flow1mirror_uni)

by countryyearB: egen totval_expBAA_cons=sum(imf_flow1_uni) if consistent_expBA==1
by countryyearB: egen totval_expBAA_cons2=max(totval_expBAA_cons)

by countryyearB: egen totval_expBAB_cons=sum(imf_flow1mirror_uni) if consistent_expBA==1
by countryyearB: egen totval_expBAB_cons2=max(totval_expBAB_cons)

by countryyearB: egen totval_expBAA_consnopr=sum(imf_flow1_uni) if consistentnopr_expBA==1
by countryyearB: egen totval_expBAA_consnopr2=max(totval_expBAA_consnopr)

by countryyearB: egen totval_expBAB_consnopr=sum(imf_flow1mirror_uni) if consistentnopr_expBA==1
by countryyearB: egen totval_expBAB_consnopr2=max(totval_expBAB_consnopr)

*
label variable totval_expBAA_consnopr2 "Monadic aggregate mirror exports, partners"
label variable totval_expBAB_consnopr2 "Monadic aggregate mirror exports, home"

keep if cowcodeAuni==2 | cowcodeAuni==20
drop if cowcodeAuni==20 & cowcodeBuni!=2

keep cowcodeBuni countryB_name_uni year num_obs_exp num_expBAA num_expBAB totval_expBAA totval_expBAB totval_expBAA_cons2 totval_expBAB_cons2 totval_expBAA_consnopr2 totval_expBAB_consnopr2
save

keep cowcodeBuni countryB_name_uni year totval_expBAA_consnopr2 totval_expBAB_consnopr2

save "C:\Users\lukas\Dropbox\Mirror replication paper\replication materials\messy\monadic\monadicexports.dta"


* MERGE IMPORTS & EXPORTS

cd "C:\Users\lukas\Dropbox\Mirror replication paper\replication materials\messy\monadic\"

use monadicimports
rename cowcodeAuni cowcode
rename countryA_name_uni country_name

generate cowcodemult=(cowcode*10000)
generate countryyear=(cowcodemult+year)
format countryyear %7.0f

save, replace


* 
use monadicexports
rename cowcodeBuni cowcode
rename countryB_name_uni country_name

generate cowcodemult=(cowcode*10000)
generate countryyear=(cowcodemult+year)
format countryyear %7.0f

save, replace
*
use monadicimports

merge 1:1 countryyear using monadicexports
drop if _merge!=3
drop _merge

save monadictrade, replace

*

keep cowcode country_name year totval_impBAA_consnopr2 totval_impBAB_consnopr2 totval_expBAA_consnopr totval_expBAB_consnopr2

label variable cowcode "COW code"
label variable country_name "Country name"
label variable year "Year"

rename totval_impBAA_consnopr2 mirrorimports_home
rename totval_impBAB_consnopr2 mirrorimports_partners

rename totval_expBAA_consnopr2 mirrorexports_partners
rename totval_expBAB_consnopr2 mirrorexports_home

* generate total trade

generate mirrortrade_home=mirrorimports_home+mirrorexports_home
generate mirrortrade_partners=mirrorimports_partners+mirrorexports_partners

label variable mirrortrade_home "Monadic aggregate mirror trade, home"
label variable mirrortrade_partners "Monadic aggregate mirror trade, partners"

* generate monadic ABBA terms

* imports
generate importABBA_relimp=((abs(mirrorimports_home-mirrorimports_partners))/((mirrorimports_home+mirrorimports_partners)/2))

label variable importABBA_relimp "imports ABBA relative to total value of imports (average of home and partner estimates)"

* exports
generate exportABBA_relexp=((abs(mirrorexports_home-mirrorexports_partners))/((mirrorexports_home+mirrorexports_partners)/2))

label variable exportABBA_relexp "exports ABBA relative to total value of exports (average of home and partner estimates)"

* total trade
generate tradeABBA_reltrade=((abs(mirrortrade_home-mirrortrade_partners))/((mirrortrade_home+mirrortrade_partners)/2))

label variable tradeABBA_reltrade "total trade ABBA relative to total value of trade (average of home and partner estimates)"

save, replace

******
* END
******